package ru.alt22.visor.php.properties;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;

public class Properties {

	public static final boolean SOURCE_FOLDER_DEFAULT = false;
	public static final String BASE_TEST_CLASS_DEFAULT = "TestCase";
	public static final String TESTER_SCRIPT_DEFAULT = "eclipse.php";

	public static final String SOURCE_FOLDER_PROPERTY = "source-folder";
	public static final String BASE_TEST_CLASS_PROPERTY = "base-test-class";
	public static final String PHP_EXE_PROPERTY = "php-exe";
	public static final String TESTER_SCRIPT_PROPERTY = "php-test-script";

	public static void setBaseTestClass(IProject project, String className) {
		setPropertyValue(project, BASE_TEST_CLASS_PROPERTY, className);
	}

	public static String getBaseTestClass(IProject project) {
		return getPropertyValue(project, BASE_TEST_CLASS_PROPERTY);
	}

	public static void setPHPExecutable(IProject project, String className) {
		setPropertyValue(project, PHP_EXE_PROPERTY, className);
	}

	public static String getPHPExecutable(IProject project) {
		return getPropertyValue(project, PHP_EXE_PROPERTY);
	}

	public static void setTestScript(IProject project, String className) {
		setPropertyValue(project, TESTER_SCRIPT_PROPERTY, className);
	}

	public static String getTestScript(IProject project) {
		return getPropertyValue(project, TESTER_SCRIPT_PROPERTY);
	}

	public static String boolToString(boolean value) {
		return Properties.boolToString(new Boolean(value));
	}

	public static String boolToString(Boolean value) {
		return value.toString();
	}

	public static Boolean stringToBoolean(String value) {
		return Boolean.valueOf(value);
	}
	
	public static String getPropertyValue(IResource resource, String property) {
		try {
			return resource.getPersistentProperty(new QualifiedName("", property));
		} catch (CoreException e) {
			return null;
		}
	}
	
	public static boolean setPropertyValue(IResource resource, String property, String value) {
		try {
			resource.setPersistentProperty(new QualifiedName("", property), value);
		} catch (CoreException e) {
			return false;
		}
		return true;
	}

	public static boolean isSourceFolder(IFolder folder) {
		return stringToBoolean(getPropertyValue(folder, SOURCE_FOLDER_PROPERTY)).booleanValue();
	}

	public static boolean isInSourceFolder(IFolder folder) {
		if (isSourceFolder(folder)) {
			return true;
		}
		if (folder.getParent() instanceof IFolder) {
			return isInSourceFolder((IFolder) folder.getParent());
		}
		return false;
	}

	public static String getTypeFileName(IFolder folder, String typeName) {
		return null;
	}

	public static IFolder getSourceFolderFor(IFolder folder) {
		if (!isInSourceFolder(folder)) {
			return null;
		}
		IFolder parent = folder;
		while (false == isSourceFolder(parent)) {
			parent = (IFolder) parent.getParent();
		}
		return parent;
	}
	
}